<?php

/**
 * Possible actions after processing:
 * 
 * - FALSE: 	**STOP** (everything)
 * - NULL: 	Not my stuff, go on
 * - ARRAY: 	Chained request. I did my part. the new tokens are:..
 * - TRUE:	All done, output/redirect. Finish.
 */
class ContentController
{
	var $Controller;
	var $sourceFile;
	var $destinationFile;
	var $tokens = array ();
	/**
	 * configures commands that are gonna be accepted as a processable request.
	 *
	 * @var mixed 
	 * TRUE every request will be accepted. 
	 * FALSE or NULL or '' will refuse every request.
	 * A not-empty string (case sensitive) will match that exact command. 
	 * 	(not that an empty string is perfectly acceptable, even if unappliccable in the case of command )
	 * An array will accept every value inside the array (case sensitive).
	 * @todo implement regular expressions (everything starting with /)
	 */
	var $commands = null;
	var $extensions = null;  //acceptable extensions. Follows the very same rule of command $property
	
	var $sourceFileCheck = true; // If True we will refuse requests for non-existing content
	
	function log( $message )
	{
		lll ( 'Driver [ '.get_class( $this ).' ]-> '.$message);
	}
	function setController ( &$Controller )
	{
		$this->Controller =& $Controller;
	}
	//--------- accepting policies
	function _acceptedCommand ()
	{
		$this->log( 'is current command acceptable ?' );
		$selected = $this->_getCommand();
		return $this->_isSelectorAcceptable( $selected, $this->commands );
	}
	function _acceptedExtension ()
	{
		$this->log( 'is current extension acceptable ?' );		
		$selected = $this->_getExtension();
		return $this->_isSelectorAcceptable( $selected, $this->extensions );
	}
	function _acceptedSourceFile ()
	{
		$source = $this->sourceFilename();
		$exists = file_exists( $source );
		if ( !$exists )
		{
			$this->log( "sourcefile '$source' does not exists.");
		}
		return true;
	}
	function _isSelectorAcceptable ( $selected, $selector )
	{
		$accepting = null;
		switch ( gettype( $selector ))
		{
			case 'boolean':
				$this->log( $selector? 'I accept everything' : "I won't accept nothing!" );
				$accepting = $selector;
			break;
			case 'string':
				$accepting = $selected == $selector ? true : null;
			break;
			case 'array':
				$accepting = in_array( $selected, $selector, true )? true :null;
			break;
		}
		if ( !$accepting )
		{
			$this->log( "'$selected' is not under of my competence, skipping" );
		} else {
			$this->log( "'$selected' accepted" );
		}
		return $accepting;
	}
	function securityCheck()
	{
		//return $this->Controller->_securityCheck();
		return true;
	}
	function _acceptRequest ()
	{
		$accepted = $this->_acceptedCommand();
		if ( !$accepted ) return $accepted;
		$accepted = $this->_acceptedExtension();
		if ( !$accepted ) return $accepted;
		if ( $this->sourceFileCheck )
		{
			$accepted = $this->_acceptedSourceFile();
			if ( !$accepted ) return $accepted;	
		}
		//$secure = $this->securityCheck();
		//if ( !$secure ) return $secure;		
		
		return true;
	}
	function _getCommand ()
	{
		return $this->tokens[0];
	}
	function _getExtension ()
	{
		$tokens = $this->tokens;
		$basename = array_pop( $tokens );
		if ( !$basename ) return '';
		$tokens = explode( '.', $basename );
		if ( count( $tokens ) < 2 ) return '';
		return array_pop( $tokens );
	}
	function _stripCommand ( $tokens )
	{
		array_shift( $tokens );
		return $tokens;
	}
	/*
	function process ( $sourceFile, $destinationFile,  $tokens )
	{
		$this->sourceFile = $sourceFile;
		$this->destinationFile = $destinationFile;
		$this->tokens = $tokens;
		$accepted = $this->_acceptRequest();
		if ( !$accepted ) return $accepted;
		return $this->generateFile();
	}
	*/
	function run( $tokens )
	{
		$this->tokens = $tokens;
		$accepted = $this->_acceptRequest();
		if ( !$accepted ) return $accepted;
				
		return $this->_processRequest();
		
	}
	function _processRequest ()
	{
		$this->log( 'not implemented' );
		return null;
	}
	function sourceFilename ()
	{
		return $this->tokens2SourceFilename( $this->_stripCommand( $this->tokens ) );
	}
	function destinationFilename ()
	{
		return $this->tokens2DestinationFilename( $this->tokens );
	}
	function tokens2SourceFilename ( $tokens )
	{
		return $this->Controller->tokens2SourceFilename( $tokens );
	}
	function tokens2DestinationFilename ( $tokens )
	{
		return $this->Controller->tokens2DestinationFilename( $tokens );
	}
}
?>